// Time:  O(n)
// Space: O(1)

#include <vector>
#include <numeric>
#include <cassert>
#include <iostream>

using namespace std;

class Solution {
public:
    vector<int> decrypt(vector<int>& code, int k) {
        vector<int> result(code.size());
        if (k == 0) {
            return result;
        }
        int left = 1, right = k;
        if (k < 0) {
            k = -k;
            left = code.size() - k;
            right = code.size() - 1;
        }
        int total = accumulate(code.cbegin() + left, code.cbegin() + right + 1, 0);
        // use the first k elements to calculate the total sum
        // then slide the window to calculate the next element
        // minus the left element and add the right element
        for (int i = 0; i < code.size(); ++i) {
            result[i] = total;
            total -= code[left++ % code.size()];
            total += code[++right % code.size()];
        }
        return result;
    }
};

int main() {
    Solution solution;

    // Test case 1: k > 0
    vector<int> code1 = {5, 7, 1, 4};
    int k1 = 3;
    vector<int> expected1 = {12, 10, 16, 13};
    assert(solution.decrypt(code1, k1) == expected1);

    // Test case 2: k < 0
    vector<int> code2 = {2, 4, 9, 3};
    int k2 = -2;
    vector<int> expected2 = {12, 5, 6, 13};
    assert(solution.decrypt(code2, k2) == expected2);

    // Test case 3: k = 0
    vector<int> code3 = {1, 2, 3, 4};
    int k3 = 0;
    vector<int> expected3 = {0, 0, 0, 0};
    assert(solution.decrypt(code3, k3) == expected3);

    // Test case 4: k > 0 with wrap around
    vector<int> code4 = {10, 5, 7, 12, 8};
    int k4 = 2;
    vector<int> expected4 = {12, 19, 20, 18, 15};
    assert(solution.decrypt(code4, k4) == expected4);

    // Test case 5: k < 0 with wrap around
    vector<int> code5 = {10, 5, 7, 12, 8};
    int k5 = -2;
    vector<int> expected5 = {20, 18, 15, 12, 19};
    assert(solution.decrypt(code5, k5) == expected5);

    return 0;
}